#!/bin/bash

data_dir="/home/data/host2/"
index_file="/home/data/genome/hg/hg19.fa.index"
script_dir="/home/data/procedure/soap/"
err_dir=$script_dir"logs/"
outdata_dir=$script_dir"data/"

cd $data_dir
rawdata=$(ls *.fastq.gz)

#use ".gz" file
for i in $rawdata
do
	# save raw '.tagAlign'
<<EOF
	#echo "block comment ... ??"
	echo $i
	origin=$(echo $i | sed -n 's/ucsc-//p')
	echo $origin
	mv $i $origin
	#exit #
EOF

#<<EOF
	tagAlign=$(echo $i | sed -n -e 's/RawData/Alignments/' -e 's/\.fastq/\.tagAlign/' -e 'p')
	echo $tagAlign
	if [ -f $tagAlign ]
	then
		echo "$tagAlign exist, move it"
		mv $tagAlign "ucsc-"$tagAlign
	fi

	tagAlign=$(echo $tagAlign | sed -n 's/\.tagAlign\.gz//p')
	echo $tagAlign
	#exit

	#SOAP
	#'-r 0'
	cd $script_dir
	soap -a $data_dir"/"$i -D $index_file -o $outdata_dir"/"$tagAlign.soap2hg19 -r 0 2>$err_dir"$i.log"  # 2: error log
	echo "done SOAP" #
	
	#to BED
	cd $outdata_dir
	$script_dir"/from_soap_output2_bed.pl" $tagAlign.soap2hg19
	echo "done BED, rm *.soap2hg19" #
	rm $tagAlign.soap2hg19 #

	#gzip it
	gzip $tagAlign.tagAlign
	cp $tagAlign.tagAlign.gz $data_dir
	echo "done GZIP with $tagAlign.tagAlign" #
#EOF

done

